﻿using System;
using Liber;
using Liber.SOA;
using Liber.BizTools;
using System.Text.RegularExpressions;

namespace SoBusiness.App
{
    public class RegisterApp:SOAUnit
    {
        public RegisterApp(SOAHost _host)
            : base(_host)
        {
        }

        [API("CreateAcnt")]
        FnResult<DyObject> CreateAccount(SOARequestContext _rq)
        {
            string acnt = _rq.RequestContext["acnt"].ToString();
            string pass = _rq.RequestContext.GetOrDefault<string>("pass", "");
            string name = _rq.RequestContext["name"].ToString();
            string phone = _rq.RequestContext.GetOrDefault<string>("phone", "");
            string headImg = _rq.RequestContext.GetOrDefault<string>("headimg", "");
            string opensrc = _rq.RequestContext.GetOrDefault<string>("opensrc", "");
            string openname = _rq.RequestContext.GetOrDefault<string>("openname", "");
            string openid = _rq.RequestContext.GetOrDefault<string>("openid", "");
            string openheadimg = _rq.RequestContext.GetOrDefault<string>("openheadimg", "");
            
            //创建账户
            var nacnt = LiberBizHelper.AcntSVC.CreateAcnt(acnt, name, pass, phone, headImg);
            if (nacnt.IsPositive)
            {
                //创建成功则再绑定第三方账号
                LiberBizHelper.AcntSVC.BindOpenUser((long)nacnt.Data["id"],opensrc, openid, openheadimg, openname);
                return nacnt;
            }
            else
            {
                return nacnt;
            }
        }

        [API("SendCpatcha")]
        FnResult<DyObject> SendCpatcha(SOARequestContext _rq)
        {
            string p = _rq.RequestContext.GetOrDefault<string>("phone", "");
            string f = _rq.RequestContext.GetOrDefault<string>("forwhat", "");
            Regex mphoneRx = new Regex(@"^[1]+[3|4|5|7|8]+\d{9}");  //用于提取手机号码的正则
            string captcha = "";
            if (string.IsNullOrEmpty(p))
            {
                return FnResult<DyObject>.Failure(Name, "RequiredArgs").SetError("phone", "phone is empty");
            }
            if (!mphoneRx.IsMatch(p))
            {
                return FnResult<DyObject>.Failure(Name, "RequiredArgs").SetError("phone", "phone format is wrong");
            }

            captcha = new Random().Next(122331, 999999).ToString();
            var obj = new DyObject().Set("code", captcha)
                                    .Set("product", "【天联云协同管理平台】");
            string smstpl = "";
            switch (f)
            {
                case "resetpwd":
                    smstpl = "SMS_5079108";
                    break;
                case "resetphone_old":
                    smstpl = "SMS_5079113";
                    break;
                case "resetphone_new":
                    smstpl = "SMS_5079107";
                    break;
                case "newacnt":
                    smstpl = "SMS_5079110";
                    break;
                default:
                    smstpl = "0";
                    break;
            }
            LiberBizHelper.SmsSVC.SendSMS(p, smstpl, obj);
            return FnResult<DyObject>.Sucess(obj.Set("captcha", captcha));
        }

        [API("BindOpenAcnt")]
        FnResult<DyObject> BindAcnt(SOARequestContext cntx)
        {
            string acnt = cntx.RequestContext.GetOrDefault<string>("acnt", "");
            string pass = cntx.RequestContext.GetOrDefault<string>("pass", "");
            string openSrc = cntx.RequestContext.GetOrDefault<string>("opensrc", "");
            string openname = cntx.RequestContext.GetOrDefault<string>("openname", "");
            string openId = cntx.RequestContext.GetOrDefault<string>("openid", "");
            string openHimg = cntx.RequestContext.GetOrDefault<string>("openheadimg", "");
            if (string.IsNullOrWhiteSpace(acnt))
            {
                return FnResult<DyObject>.Failure(Name, "RequiredArgs").SetError("acnt", "EmptyAcnt");
            }
            return LiberBizHelper.AcntSVC.BindOpenUser(acnt, pass, openname,openSrc, openId, openHimg);

        }

        [API("ResetPwd")]
        FnResult<DyObject> ResetPwd(SOARequestContext rq)
        {
            string phone = rq.RequestContext.GetOrDefault<string>("phone","");
            string pwd = rq.RequestContext.GetOrDefault<string>("newpass", "");

            var r= LiberBizHelper.AcntSVC.ResetPwd(phone, pwd);
            if (r.IsPositive)
            {
                return FnResult<DyObject>.Sucess(null);
            }
            else
            {
                return FnResult<DyObject>.Failure(r);
            }
        }

    }
}
